package com.pmgames.dao;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.net.URL;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import com.pmgames.dao.IPmgamesDao;

public abstract class PmgamesBaseService implements Controller {

	private IPmgamesDao pmgamesDao;
	
	public PmgamesBaseService() {
		// TODO Auto-generated constructor stub
	}
	
	@Override
	public ModelAndView handleRequest(HttpServletRequest arg0,
			HttpServletResponse arg1) throws Exception {
		String xmlString = "";
		try {
			xmlString = getResultXmlHeader();
			xmlString += getXmlFromOperation(arg0,arg1);
			xmlString += getResultXmlFooter();
			
			// Create file 
		    FileWriter fstream = new FileWriter("F:/Diplomna/Logs/DBoutput.xml");
		    BufferedWriter out = new BufferedWriter(fstream);
		    out.write(xmlString);
		    out.close();
		} catch (Exception e) {
			xmlString = getErrorXml(e.getMessage());
		}
		return new ModelAndView("WEB-INF/jsp/xml.jsp", "xmlString",xmlString);
	}
	
	protected String getErrorXml(String message)
	{
		String errorXml = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>";
		errorXml += "<result type=\"error\">";
		errorXml += "<message>";
		errorXml += message;
		errorXml += "</message>";
		errorXml += "</result>";
		return errorXml;
	}
	
	protected String getResultXmlHeader()
	{
		String resultXml = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>";
		resultXml += "<result type=\"success\">";
		return resultXml;
	}
	
	protected abstract String getXmlFromOperation
		(HttpServletRequest request, HttpServletResponse response) 
		throws Exception;
	
	protected String getResultXmlFooter()
	{
	return "</result>";
	}
	
	protected static String getTagValue(String sTag, Element eElement){
	    NodeList nlList= eElement.getElementsByTagName(sTag).item(0).getChildNodes();
	    Node nValue = (Node) nlList.item(0); 
	 
	    return nValue.getNodeValue();    
	 }
	
	protected Document getXmlFromRequest(HttpServletRequest request)
	{
		Document doc = null;
		try
		{
			String url = getUrlFromRequest(request);
			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
			DocumentBuilder db = dbf.newDocumentBuilder();
			doc = db.parse(new URL(url).openStream());
			
		}
		catch (Exception e) {
			System.out.println("XML Pasing Excpetion: " + e);
		}
		return doc;
		
	}
	
	private static String getUrlFromRequest(HttpServletRequest request) 
	{
	    String reqUrl = request.getRequestURL().toString();
	    String queryString = request.getQueryString();   
	    if (queryString != null) 
	    {
	       // reqUrl += "?"+queryString;
	    }
	    return reqUrl;
	}
	

	
	public IPmgamesDao getPmgamesDao() {
		return pmgamesDao;
	}
	
	public void setPmgamesDao(IPmgamesDao pmgamesDao) {
		this.pmgamesDao = pmgamesDao;
	}

}
